// This whole app, for all intents and purposes sucks right now. But it'll get better, I promise!
const Cc = Components.classes;
const Ci = Components.interfaces;
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

var mozDiagMain = {
  
  _inited: false,
  _prefBranch: null,
  _platform: null,
  _vendor: null,
  _product: null,
  
  init: function () {
    if (this._inited)
      return;
    //Equal to the OS_TARGET configure variable -- Not frozen and (as of this writing) doesn't work?
    var os = Cc["@mozilla.org/xre/app-info;1"].
             getService(Ci.nsIXULRuntime).OS;
    this._platform = os;
    this._vendor = "Mozilla";
    this._product = "Firefox";
    this._inited = true;

    this.populatePackages();
    
    document.getElementById('browser').goHome();
  },

  populatePackages: function () {
    var dir = Cc["@mozilla.org/file/directory_service;1"].
                getService(Ci.nsIProperties).
                get("resource:app", Ci.nsIFile);
    dir.append("chrome");
    dir.append("fxdiag");
    dir.append("packages");
    
    var dirs = dir.directoryEntries;
    var packs = [];
    while(dirs.hasMoreElements()) {
      var pack = dirs.getNext();
      pack.QueryInterface(Ci.nsIFile);
      packs.push(pack);
    }
    
    var ios = Cc["@mozilla.org/network/io-service;1"].
              getService(Ci.nsIIOService);
    var fileHandler = ios.getProtocolHandler("file")
                         .QueryInterface(Ci.nsIFileProtocolHandler);

    for (var i = 0; i < packs.length; i++) {
      var info = packs[i];
      var baseURL = fileHandler.getURLSpecFromFile(info);
      info.append("packageInfo.xml");
 
      if (info.exists()) {
        var xmlURL = fileHandler.getURLSpecFromFile(info);
        var xhr = new XMLHttpRequest();
        xhr.open("GET", xmlURL, false); 
        xhr.send(null);
        var rtxt = xhr.responseXML;

        if (!document.getElementById("foxdoc-main"))
          return;

        var treechild = document.getElementById("packages");
        var treeitem = document.createElementNS(XUL_NS, "treeitem");
        var treerow = document.createElementNS(XUL_NS, "treerow");
        var treecell1 = document.createElementNS(XUL_NS, "treecell");
        var treecell2 = document.createElementNS(XUL_NS, "treecell");

        treecell1.setAttribute("label", rtxt.getElementsByTagName('name').item(0).firstChild.data);
        treecell2.setAttribute("label", baseURL + "/" + rtxt.getElementsByTagName('startpage').item(0).firstChild.data);
        treerow.appendChild(treecell1);
        treerow.appendChild(treecell2);
        treeitem.appendChild(treerow);
        treechild.appendChild(treeitem);
      }
    }
  },
  
  onTreeSelect: function (event) {
    var tree = event.target;
    var url = tree.view.getCellText(tree.currentIndex, tree.columns["urlColumn"]);
    document.getElementById("browser").setAttribute("src", url);
  },
    
  winRegScan: function () {
    var product = "Mozilla Firefox";
    var reg = Cc["@mozilla.org/windows-registry-key;1"].
                createInstance(Ci.nsIWindowsRegKey);
    
    reg.open(reg.ROOT_KEY_CURRENT_USER, "", reg.ACCESS_READ);
    var ver = reg.openChild("Software\\Mozilla\\" + product, reg.ACCESS_READ);
    var cv = ver.readStringValue("CurrentVersion");
    var main = reg.openChild("Software\\Mozilla\\" + product + "\\" + cv + "\\Main", reg.ACCESS_READ);
    var inst = main.readStringValue("Install Directory");
    reg.close();
    
    return [cv, inst];
  },
    
  getAppDataFolder: function () {
    var file = Cc["@mozilla.org/file/directory_service;1"].
               getService(Ci.nsIProperties).
               get("AppData", Ci.nsIFile);
    return file;
  },
  
  getProfileFolder: function () {
    // We have a profile dir so just move up to wherever profiles are on this system.
    var profile = Cc["@mozilla.org/file/directory_service;1"].
                  getService(Ci.nsIProperties).
                  get("ProfD", Ci.nsIFile);
    profile = profile.parent.parent.parent;
    profile.append(this._vendor);
    profile.append(this._product);
    return profile;
  },
  
  searchProfileFolder: function () {
    
  }
};

window.addEventListener("load", function(event) { mozDiagMain.init(event); }, false);